package com.toocms.jinjuhang.ui.mine.management_system

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.OrganizationData
import com.toocms.jinjuhang.data.system.ShareLinkData
import com.toocms.jinjuhang.ui.mine.contact_us.ContactUsFgt
import com.toocms.jinjuhang.ui.mine.management_system.institution_address.InstitutionAddressFgt
import com.toocms.jinjuhang.ui.mine.management_system.order_details.ManagementSystemOrderDetailsFgt
import com.toocms.jinjuhang.ui.mine.management_system.sales_income.SalesIncomeFgt
import com.toocms.jinjuhang.ui.mine.management_system.vip_center.VipCenterFgt
import com.toocms.jinjuhang.ui.mine.management_system.withdraw.WithdrawFgt
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.share.TabShare
import com.umeng.socialize.bean.SHARE_MEDIA

class ManagementSystemModel(application: Application) : BaseViewModel<BaseModel>(application) {
    val dataObservableField = ObservableField<OrganizationData>()
    var link: String? = null
    val bundle = Bundle()
    val functionItems by lazy {
        val result = ObservableArrayList<ManagementSystemFunctionItemModel>()
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_popularize_business_card)),
            ObservableField(StringUtils.getString(R.string.str_popularize_business_card)),
            clickBindingCommand = BindingCommand<BindingAction>(BindingAction {
//                PromoteBusinessCardDialog().show(
//                    (ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null
//                )
                if (link.isNullOrEmpty()) return@BindingAction
                TabShare.getOneKeyShare()
                    .setPlatform(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                    .setUrl(
                        link,
                        "邀请加入",
                        "${UserRepository.user.real_name}邀请您加入华盟公益",
                        R.mipmap.icon_logo
                    )
                    .share()
            })
        )
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_vip_center)),
            ObservableField(StringUtils.getString(R.string.str_vip_center)),
            openFgt = VipCenterFgt::class.java,
            bundle = bundle
        )

        if ("2" != UserRepository.user.level) {
            result += ManagementSystemFunctionItemModel(
                this,
                ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_sales_income)),
                ObservableField(StringUtils.getString(R.string.str_sales_income)),
                openFgt = SalesIncomeFgt::class.java,
                bundle = bundle
            )
        }
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_order_details)),
            ObservableField(StringUtils.getString(R.string.str_order_details)),
            openFgt = ManagementSystemOrderDetailsFgt::class.java,
            bundle = bundle
        )
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_my_equities)),
            ObservableField(StringUtils.getString(R.string.str_my_equities))
        )
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_my_facilitating_agency)),
            ObservableField(StringUtils.getString(R.string.str_my_facilitating_agency)),
            openFgt = ContactUsFgt::class.java,
            bundle = bundle.apply {
                putString(
                    Constants.BundleKey.KEY_TITLE,
                    StringUtils.getString(R.string.str_my_facilitating_agency)
                )
            }
        )
        result += ManagementSystemFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_agency_collection_address)),
            ObservableField(StringUtils.getString(R.string.str_agency_collection_address)),
            openFgt = InstitutionAddressFgt::class.java,
            bundle = bundle
        )
        result
    }
    val functionItemBinding = ItemBinding.of<ManagementSystemFunctionItemModel>(
        BR.managementSystemFunctionItemModel, R.layout.item_management_system_function
    )


    val withdrawClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val params = Bundle()
        params.putString(Constants.BundleKey.KEY_BALANCE, dataObservableField.get()?.balance)
        startFragment(WithdrawFgt::class.java, params)
    })

    init {
        requestData(true)
        initializeMessenger()
    }


    override fun onStop() {
        super.onStop()
        TabShare.release()
    }

    private fun initializeMessenger() {
        Messenger.getDefault().register(this, Constants.MessengerToken.TOKEN_REFRESH_BALANCE) {
            requestData()
        }
    }


    private fun requestData(isShowLoading: Boolean = false) {
        val params = mapOf(Pair("member_id", UserRepository.user.member_id))
        requestData(
            Urls.System.SHARE_LINK,
//                    mapOf(Pair("member_id", UserRepository.user.member_id)),
            params,
            resultClass = ShareLinkData::class.java,
            result = {
                link = it.link
            })
        requestData(Urls.Center.ORGANIZATION,
            params,
            resultClass = OrganizationData::class.java,
            isShowLoading = isShowLoading,
            result = {
                bundle.putString(Constants.BundleKey.KEY_ORGANIZATION_ID, it.organization_id)
                bundle.putString(Constants.BundleKey.KEY_TYPE, it.type)
                bundle.putString(Constants.BundleKey.KEY_MEMBERS, it.members)
                bundle.putString(Constants.BundleKey.KEY_COMMISSIONER, it.commissioner)
                bundle.putString(Constants.BundleKey.KEY_BROKERAGE, it.brokerage)
                bundle.putString(Constants.BundleKey.KEY_ORDER_AMOUNTS, it.order_amounts)
                bundle.putString(Constants.BundleKey.KEY_ORDERS, it.orders)
                bundle.putString(Constants.BundleKey.KEY_INCOME, it.income)
                bundle.putParcelable(Constants.BundleKey.KEY_ADDRESS, it.address)
                dataObservableField.set(it)
            })
    }

}